# This file is part of Sonedyan.
#
# Sonedyan is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 3 of the License, or (at your option) any
# later version.
#
# Sonedyan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2009-2012 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>

library(igraph)
library(hash)
#library(entropy)

source("metrics.R")

###

# set graph to be used

# FA Core
#g <- read.graph(file = "fa-core-graphml.xml", format = "graphml")

# FA ER
# method 1: Gnm random graph
#g <- erdos.renyi.game(4843, 61544, type = "gnm", directed = TRUE)
#V(g)$id <- paste(c(1:length(V(g))), sep = "")
#E(g)$id <- paste(c(1:length(E(g))), sep = "")

# Barabasi model
# NB: the graph IS directed
# average degree: 61544 / 4843
mu <- 12.708
norm <- function(x){ exp(-(x-mu)^2)/2}
dist <- sapply(seq(0,50), norm)
g <- barabasi.game(4843, out.dist = dist)
V(g)$id <- paste(c(1:length(V(g))), sep = "")
E(g)$id <- paste(c(1:length(E(g))), sep = "")

# DD Core
#g <- read.graph(file = "dd-core-graphml.xml", format = "graphml")

###

# set of cycle lengths
lv <- c(2,3)

vtot <- length(V(g))
etot <- length(E(g))
# number of vertices/edges to be deleted at each step
nv <- round(vtot/10)
ev <- round(etot/10)

# vertex / edge random attack
for (i in c(1:10))
{
  cje <- get.graph.cycles.joint.entropy(g, lv)  
  print(paste("Cycles joint entropy: ", cje))
  
  gstats <- get.graph.stats(g, c("avgsp", "msccs", "ddiam", "udiam", "gtrans", "eff", "dens"))
  
  print(paste("Avg shortest path: ", gstats[["avgsp"]]))
  print(paste("Main SCC size: ", gstats[["msccs"]]))
  
  print(paste("Diameter (directed): ", gstats[["ddiam"]]))
  print(paste("Diameter (undirected): ", gstats[["udiam"]]))
  print(paste("Transitivity (CC): ", gstats[["gtrans"]]))
  print(paste("Efficiency: ", gstats[["eff"]]))

  density <- gstats[["dens"]]
  print(paste("Density: ", density, "(n*p = ", length(V(g))*density, ")"))

  #g <- vertex.random.attack(g, nv)
  g <- edge.random.attack(g, ev)
}

#print(paste("FA entropy: ", ge))
#print(paste("FA-ER joint entropy: ", entropy(sm2, method = "shrink")))
